package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.model.cart.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 卡尼奶
 * @Date 2022/6/1 18:15
 * @Version 1.0
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @GetMapping("/cart.html")
    public String cartPage(){
        return "cart/index";
    }

    /**
     * 添加商品到购物车
     * @param skuId
     * @param skuNum
     * @param model
     * @return
     */
    @GetMapping("/addCart.html")
    public String addCart(
            @RequestParam("skuId") Long skuId,
            @RequestParam("skuNum") Integer skuNum,
            Model model){


        Result<CartItem> result = cartFeignClient.addToCart(skuId, skuNum);
        if (result.isOk()){
            CartItem item = result.getData();
            model.addAttribute("skuInfo",item);
            model.addAttribute("skuNum",item.getSkuNum());
        }

        return "cart/addCart";
    }
}
